﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.Packets
{
    public class ConnectionPacket : Packet
    {
        private byte adresseSource;
        private byte adresseDestination;

        private ConnectionPacket(byte numeroConnexion, byte typePacket, byte adresseSource, byte adresseDestination)
        {
            this.numeroConnexion = numeroConnexion;
            this.typePacket = typePacket;
            this.adresseSource = adresseSource;
            this.adresseDestination = adresseDestination;
        }

        public byte AdresseSource { get { return adresseSource; } }
        public byte AdresseDestination { get { return adresseDestination; } }

        public static ConnectionPacket CreateRequestConnectionPacket(int numeroConnexion, int adresseSource, int adresseDestination)
        {
            ConnectionPacket requestPacket = new ConnectionPacket(Convert.ToByte(numeroConnexion), 
                                                            (byte)PacketTypes.RequestConnectionPacket, 
                                                            Convert.ToByte(adresseSource), 
                                                            Convert.ToByte(adresseDestination));
            return requestPacket;
        }
        public static ConnectionPacket CreateConfirmationConnectionPacket(int numeroConnexion, int adresseSource, int adresseDestination)
        {
            ConnectionPacket requestPacket = new ConnectionPacket(Convert.ToByte(numeroConnexion),
                                                            (byte)PacketTypes.ConfirmationConnectionPacket,
                                                            Convert.ToByte(adresseSource),
                                                            Convert.ToByte(adresseDestination));
            return requestPacket;
        }

        

        public override string GetStringOfBitArray()
        {
            string arrayOfBits = base.GetStringOfBitArray();
            arrayOfBits += Convert.ToString(adresseSource, 2).PadLeft(8, '0');
            arrayOfBits += Convert.ToString(adresseDestination, 2).PadLeft(8, '0');
            return arrayOfBits;
        }
    }
}
